package com.xin.di.uav.mapper;

import com.xin.di.uav.po.SpecificationPO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface SpecificationDao {

    @Select({"<script>",
            "SELECT id, product_id, value2, value3, value, sale_price, created_at, updated_at, supply_price,\n" +
                    "                            stock, value4, value5 FROM specifications where product_id in " +
                    "<foreach collection='productIds' item='productId' open='(' close=')' separator=','>",
            "#{productId}",
            "</foreach>",
                    " order by sale_price ",
            "</script>"})
    List<SpecificationPO> selByProductIds(@Param("productIds") List<String> productIds);

    @Select({"<script>",
            "SELECT id, product_id, value2, value3, value, sale_price, created_at, updated_at, supply_price,\n" +
                    "                            stock, value4, value5 FROM specifications where id in " +
                    "<foreach collection='ids' item='id' open='(' close=')' separator=','>",
            "#{id}",
            "</foreach>",
            " order by sale_price ",
            "</script>"})
    List<SpecificationPO> selByIds(@Param("ids") List<String> ids);

    @Select({"<script>",
            "select count(1) \n" +
                    "from specifications where product_id=#{productId} order by created_at desc",
            "</script>"})
    Integer countListByProductId(@Param("productId") String productId);

    @Select({"<script>",
            "select id, product_id, value2, value3, value, sale_price, created_at, updated_at, supply_price,\n" +
                    "                            stock, value4, value5\n" +
                    "from specifications where product_id=#{productId} order by created_at desc limit #{offset},#{pageSize}",
            "</script>"})
    List<SpecificationPO> selListByProductId(@Param("productId") String productId,int offset,int pageSize);

    @Insert({"<script>",
            "insert into specifications (id, product_id, value2, value3, value, sale_price,supply_price,\n" +
                    "                            stock, value4, value5)\n" +
                    "values (#{id}, #{productId}, #{value2}, #{value3}, #{value}, #{salePrice},#{supplyPrice}," +
                    "                                             #{stock}, #{value4}, #{value5})",
            "</script>"})
    int insert(SpecificationPO specificationPO);


    @Delete({"<script>",
            "delete from specifications where product_id=#{productId}",
            "</script>"})
    int deleteByProductId(String productId);


    @Update({
            "<script>",
            "UPDATE specifications ",
            "<set>",
            "    <if test='stock != null'>stock = #{stock},</if>",
            "    updated_at = NOW()",

            "</set>",
            "WHERE id = #{id}",
            "</script>"
    })
    int updateStock(SpecificationPO specificationPO);
}
